package com.matrix.draft.rabbitmqspringboot.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.matrix.draft.rabbitmqspringboot.config.AdvanceRabbitMQConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.IntStream;

/**
 * 发布确认(高级)的前端控制器
 */
@RestController
@RequestMapping("/confirm")
public class AdvanceConfirmController {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/{msg}")
    String produceConfirm(@PathVariable("msg") String msg) {
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(IdUtil.fastUUID());
        rabbitTemplate.convertAndSend(
                AdvanceRabbitMQConfig.exchangeConfirm,
                AdvanceRabbitMQConfig.routingKey,
                msg,
                correlationData
        );
        return StrUtil.format("消息\"{}\"发送成功", msg);
    }

    @RequestMapping("/ttl/{msg}")
    String produceConfirmTtl(@PathVariable("msg") String msg) {
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(IdUtil.fastUUID());
        rabbitTemplate.convertAndSend(
                AdvanceRabbitMQConfig.exchangeConfirm,
                AdvanceRabbitMQConfig.routingKey,
                msg,
                m -> {
                    m.getMessageProperties().setDelay(10000);
                    return m;
                },
                correlationData
        );
        return StrUtil.format("消息\"{}\"发送成功", msg);
    }

    // 测试备份交换机
    @RequestMapping("/spare/{msg}")
    String alternate(@PathVariable("msg") String msg) {
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(IdUtil.fastUUID());
        rabbitTemplate.convertAndSend(
                AdvanceRabbitMQConfig.exchangeConfirm,
                AdvanceRabbitMQConfig.routingKey+"123",
                msg,
                correlationData
        );
        return StrUtil.format("消息\"{}\"已发送", msg);
    }

    // 测试消息优先级
    @RequestMapping("/priority/{msg}")
    String priority(@PathVariable("msg") String msg){
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(IdUtil.fastSimpleUUID());
        // 发10条
        IntStream.range(1,11).forEach(j->{
            final int i = j;
            rabbitTemplate.convertAndSend(
                    AdvanceRabbitMQConfig.exchangeConfirm,
                    AdvanceRabbitMQConfig.routingKey,
                    msg+i,
                    m -> {
                        m.getMessageProperties().setPriority(i==8?i:0);
                        return m;
                    },
                    correlationData
            );
        });

        return "优先级消息发送完毕";
    }

}
